package com.example.stock.business;

import com.example.stock.controller.dto.WeekLowStaDTO;
import com.example.stock.controller.vo.StockInfoVO;
import com.example.stock.model.WeekDetail;
import com.example.stock.service.StockService;
import com.example.stock.service.WeekDataService;
import com.example.stock.util.DateUtil;
import com.example.stock.util.NumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 接近前低点
 */
@Component
public class WeekLowStaBusiness {

    @Autowired
    WeekDataService weekDataService;

    @Autowired
    StockService stockService;

    public List<WeekLowStaDTO> getWeekLowSta() {

        List<WeekLowStaDTO> ret = new ArrayList<>();

        stockService.getAllStocksName().forEach((stock) -> {
            List<WeekDetail> rawData = weekDataService.getWeekRawData(stock);
            if (rawData == null || rawData.isEmpty()) {
                return;
            }

            // 最近的 150 项数据
            List<WeekDetail> df = rawData.stream().skip(Math.max(0, rawData.size() - 150)).toList();

            WeekDetail current = df.getLast();
            WeekDetail lowest = df.stream().min(Comparator.comparing(WeekDetail::getClose)).get();

            StockInfoVO stockInfo = stockService.getStockInfo(stock);

            WeekLowStaDTO weekLowStaDTO = new WeekLowStaDTO();
            weekLowStaDTO.setStock(stock);
            weekLowStaDTO.setCurrentDate(current.getDate());
            weekLowStaDTO.setLowestDate(lowest.getDate());
            weekLowStaDTO.setClose(current.getClose());
            weekLowStaDTO.setWeekPct(current.getWeekPct());
            weekLowStaDTO.setLp(current.getLp());
            weekLowStaDTO.setIndustry(stockInfo.getIndustry());
            weekLowStaDTO.setCategory(stockInfo.getCategory());
            weekLowStaDTO.setWg(stockInfo.getWg());
            weekLowStaDTO.setDisPct(NumberUtil.calPct(current.getClose(), lowest.getClose()));
            weekLowStaDTO.setLowestDuration(DateUtil.calWeekDataDuration(current.getDate(), lowest.getDate()));

            ret.add(weekLowStaDTO);
        });

        return ret;
    }
}
